<?php


class Controller_ProjectProp extends Controller_Website {
    
    public function action_list() {
        $m_projectProp = Model::factory('projectProp');
        
        $where = array();
        $where['ORDER'] = 'id ASC';
        $total = $m_projectProp->count($where);
        $pager = new Pager($total, 20);
        $list = $m_projectProp->select($pager->offset, $pager->size,$where)->as_array();
        
        $m_projectCategory = Model::factory('projectCategory');
        $where = array();
        $where['parent_id'] = 1;
        $columns = 'name,id';
        $c_list = $m_projectCategory->getAll($where,$columns)->as_array();
        
        foreach ($list as &$item) {
            foreach ($c_list as &$c_item) {
                if ($item['project_cid']==$c_item['id']) {
                    $item['project_Category']=$c_item['name'];
                }
            }
        }
//         var_dump($list);exit;
        $this->content = View::factory('projectProp/list');
        $this->content->list = $list;
        $this->content->pager = $pager;

    }
    
    public function action_add() {
        $m_projectProp = Model::factory('projectProp');
        
        if (!empty($_POST)) {
//             $data = $_POST;

            $data['project_cid'] = $_POST['project_cid'];
            $data['project_budget'] = $_POST['project_budget'];
            $data['Service_fee'] = $_POST['Service_fee'];
            $data['Taxes_and_fees'] = $_POST['Taxes_and_fees'];
            
            $tm['Planning_Writer'] = array('type'=>$_POST['Planning_Writer']>1?'1':'2','cost'=>$_POST['Planning_Writer'],'num'=>$_POST['Planning_Writer1']);
            $data['prop_before'] = json_encode($tm);
            $data['prop_before_sum'] = $_POST['prop_before_sum'];

            $tmp['director'] = array('type'=>$_POST['director']>1?'1':'2','cost'=>$_POST['director'],'num'=>$_POST['director1']);
            $tmp['Photographer'] = array('type'=>$_POST['Photographer']>1?'1':'2','cost'=>$_POST['Photographer'],'num'=>$_POST['Photographer1']);
            $tmp['Aerial_photographer'] = array('type'=>$_POST['Aerial_photographer']>1?'1':'2','cost'=>$_POST['Aerial_photographer'],'num'=>$_POST['Aerial_photographer1']);
            $tmp['Photography_Assistant'] = array('type'=>$_POST['Photography_Assistant']>1?'1':'2','cost'=>$_POST['Photography_Assistant'],'num'=>$_POST['Photography_Assistant1']);
            $tmp['art'] = array('type'=>$_POST['art']>1?'1':'2','cost'=>$_POST['art'],'num'=>$_POST['art1']);
            $tmp['Gaffer'] = array('type'=>$_POST['Gaffer']>1?'1':'2','cost'=>$_POST['Gaffer'],'num'=>$_POST['Gaffer1']);
            $tmp['Lighting_Assistant'] = array('type'=>$_POST['Lighting_Assistant']>1?'1':'2','cost'=>$_POST['Lighting_Assistant'],'num'=>$_POST['Lighting_Assistant1']);
            $tmp['Makeup_artist'] = array('type'=>$_POST['Makeup_artist']>1?'1':'2','cost'=>$_POST['Makeup_artist'],'num'=>$_POST['Makeup_artist1']);
            $tmp['Stylist'] = array('type'=>$_POST['Stylist']>1?'1':'2','cost'=>$_POST['Stylist'],'num'=>$_POST['Stylist1']);
            $tmp['Props'] = array('type'=>$_POST['Props']>1?'1':'2','cost'=>$_POST['Props'],'num'=>$_POST['Props1']);
            $tmp['Cannon_Operator'] = array('type'=>$_POST['Cannon_Operator']>1?'1':'2','cost'=>$_POST['Cannon_Operator'],'num'=>$_POST['Cannon_Operator1']);
            $tmp['sound_engineer'] = array('type'=>$_POST['sound_engineer']>1?'1':'2','cost'=>$_POST['sound_engineer'],'num'=>$_POST['sound_engineer1']);
            $tmp['Recording_Assistant'] = array('type'=>$_POST['Recording_Assistant']>1?'1':'2','cost'=>$_POST['Recording_Assistant'],'num'=>$_POST['Recording_Assistant1']);
            $tmp['Fieldwork'] = array('type'=>$_POST['Fieldwork']>1?'1':'2','cost'=>$_POST['Fieldwork'],'num'=>$_POST['Fieldwork1']);
            $tmp['actor'] = array('type'=>$_POST['actor']>1?'1':'2','cost'=>$_POST['actor'],'num'=>$_POST['actor1']);
            $data['prop_middle'] = json_encode($tmp);
//             $data['prop_middle_sum'] = $_POST['director']+$_POST['Photographer']+$_POST['Aerial_photographer']
//                 +$_POST['Photography_Assistant']+$_POST['Photography_Assistant']+$_POST['art']+$_POST['Gaffer']+$_POST['Lighting_Assistant']
//                 +$_POST['Makeup_artist']+$_POST['Stylist']+$_POST['Props']+$_POST['Cannon_Operator']+$_POST['sound_engineer']
//                 +$_POST['Recording_Assistant']+$_POST['Fieldwork']+$_POST['actor'];
//             var_dump($data['prop_middle_sum']);exit;
            $tmp2['camera'] = array('type'=>$_POST['camera']>1?'1':'2','cost'=>$_POST['camera'],'num'=>$_POST['camera1']);
            $tmp2['Lens'] = array('type'=>$_POST['Lens']>1?'1':'2','cost'=>$_POST['Lens'],'num'=>$_POST['Lens1']);
            $tmp2['Record_storage'] = array('type'=>$_POST['Record_storage']>1?'1':'2','cost'=>$_POST['Record_storage'],'num'=>$_POST['Record_storage1']);
            $tmp2['Recording_equipment'] = array('type'=>$_POST['Recording_equipment']>1?'1':'2','cost'=>$_POST['Recording_equipment'],'num'=>$_POST['Recording_equipment1']);
            $tmp2['light'] = array('type'=>$_POST['light']>1?'1':'2','cost'=>$_POST['light'],'num'=>$_POST['light1']);
            $tmp2['Load_rocker'] = array('type'=>$_POST['Load_rocker']>1?'1':'2','cost'=>$_POST['Load_rocker'],'num'=>$_POST['Load_rocker1']);
            $tmp2['track'] = array('type'=>$_POST['track']>1?'1':'2','cost'=>$_POST['track'],'num'=>$_POST['track1']);
            $tmp2['Stabilizer'] = array('type'=>$_POST['Stabilizer']>1?'1':'2','cost'=>$_POST['Stabilizer'],'num'=>$_POST['Stabilizer1']);
            $tmp2['Sports_car'] = array('type'=>$_POST['Sports_car']>1?'1':'2','cost'=>$_POST['Sports_car'],'num'=>$_POST['Sports_car1']);
            $tmp2['Transporter'] = array('type'=>$_POST['Transporter']>1?'1':'2','cost'=>$_POST['Transporter'],'num'=>$_POST['Transporter1']);
            $data['prop_middle2'] = json_encode($tmp2);
//             $data['prop_middle2_sum'] = $_POST['camera']+$_POST['Lens']+$_POST['Record_storage']+$_POST['Recording_equipment']
//                 +$_POST['light']+$_POST['Load_rocker']+$_POST['track']+$_POST['Stabilizer']+$_POST['Sports_car']
//                 +$_POST['Transporter'];

            $tmp3['Scenes'] = array('type'=>$_POST['Scenes']>1?'1':'2','cost'=>$_POST['Scenes'],'num'=>$_POST['Scenes1']);
            $tmp3['Scenery'] = array('type'=>$_POST['Scenery']>1?'1':'2','cost'=>$_POST['Scenery'],'num'=>$_POST['Scenery1']);
            $data['prop_middle3'] = json_encode($tmp3);
//             $data['prop_middle3_sum'] = $_POST['Scenes']+$_POST['Scenery'];
            
            $tmp4['Mixer'] = array('type'=>$_POST['Mixer']>1?'1':'2','cost'=>$_POST['Mixer'],'num'=>$_POST['Mixer1']);
            $tmp4['Voice_over'] = array('type'=>$_POST['Voice_over']>1?'1':'2','cost'=>$_POST['Voice_over'],'num'=>$_POST['Voice_over1']);
            $tmp4['Music_Maker'] = array('type'=>$_POST['Music_Maker']>1?'1':'2','cost'=>$_POST['Music_Maker'],'num'=>$_POST['Music_Maker1']);
            $tmp4['Clip_packaging'] = array('type'=>$_POST['Clip_packaging']>1?'1':'2','cost'=>$_POST['Clip_packaging'],'num'=>$_POST['Clip_packaging1']);
            $tmp4['animation'] = array('type'=>$_POST['animation']>1?'1':'2','cost'=>$_POST['animation'],'num'=>$_POST['animation1']);
            $tmp4['Colorist'] = array('type'=>$_POST['Colorist']>1?'1':'2','cost'=>$_POST['Colorist'],'num'=>$_POST['Colorist1']);
            $data['prop_after'] = json_encode($tmp4);
            $data['prop_after_sum'] = $_POST['prop_after_sum'];
//             $data['prop_after_sum'] = $_POST['Mixer']+$_POST['Voice_over']+$_POST['Music_Maker']
//                 +$_POST['Clip_packaging_animation']+$_POST['Colorist'];
// var_dump($data);exit;

            $ret = $m_projectProp->insert($data);
            if ($ret !== false) {
                $this->redirect('projectProp/list');
            }
        }
        
        $m_projectCategory = Model::factory('projectCategory');
        $where = array();
        $where['parent_id'] = 1;
        $columns = 'name,id';
        $c_list = $m_projectCategory->getAll($where,$columns)->as_array();
        
        
        
        $this->content = View::factory('projectProp/add');
        $this->content->c_list = $c_list;
    }
    
    public function action_del() {
        $id = $_GET['id'];
        $m_projectProp = Model::factory('projectProp');
        $ret = $m_projectProp->deleteById($id);
//         var_dump($ret);exit;
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_detail() {
        $id = $_GET['id'];
        $m_projectProp = Model::factory('projectProp');
        $projectProp = $m_projectProp->getRowById($id);
        
        $m_projectCategory = Model::factory('projectCategory');
        $where = array();
        $where['parent_id'] = 1;
        $columns = 'name,id';
        $projectCategory = $m_projectCategory->getAll($where,$columns)->as_array();
        
        
        foreach ($projectCategory as &$c_item) {
            if ($projectProp['project_cid']==$c_item['id']) {
                $projectProp['project_Category']=$c_item['name'];
            }
        }
        
        $projectProp['prop_before'] = json_decode($projectProp['prop_before'], true);
        $projectProp['prop_middle'] = json_decode($projectProp['prop_middle'], true);
        $projectProp['prop_middle2'] = json_decode($projectProp['prop_middle2'], true);
        $projectProp['prop_middle3'] = json_decode($projectProp['prop_middle3'], true);
        $projectProp['prop_after'] = json_decode($projectProp['prop_after'], true);
        
//         var_dump($projectProp['prop_middle']);exit;
        
        $this->content = View::factory('projectProp/detail');
        $this->content->projectProp = $projectProp;
    }
    
    protected function _get_data($post) {
        return array_intersect_key($post, array_flip(array('project_cid','project_budget','prop_before','prop_middle','prop_middle2','prop_middle3','prop_after')));
    }
}